﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace CharHelp
{
    public static class CharUtils
    {
        #region 显示配置
        /// <summary>
        /// X轴最大值
        /// </summary>
        private static int Max_X { get; set; } = 0;
        /// <summary>
        /// Y轴最大值
        /// </summary>
        private static int Max_Y { get; set; } = 0;
        /// <summary>
        /// 图像边距，默认上、下、左、右各30
        /// </summary>
        public static int Margin { get; set; } = 60;
        /// <summary>
        /// 刻度
        /// </summary>
        public static int Scale { get; set; } = 20;
        /// <summary>
        /// 点的大小
        /// </summary>
        public static int PointSize { get; set; } = 6;
        /// <summary>
        /// 线的宽度
        /// </summary>
        public static int LineSize { get; set; } = 2;
        /// <summary>
        /// 是否显示刻度
        /// </summary>
        public static bool IsShowScale { get; set; } = true;
        /// <summary>
        /// 是否显示网格
        /// </summary>
        public static bool IsShowGrid { get; set; } = true;
        /// <summary>
        /// 是否显示数值标题
        /// </summary>
        public static bool IsShowTitle { get; set; } = false;
        /// <summary>
        /// 边框颜色
        /// </summary>
        public static Color BorderColor { get; set; } = Color.Black;
        /// <summary>
        /// 背景颜色
        /// </summary>
        public static Brush BackColor { get; set; } = Brushes.White;
        /// <summary>
        /// 网格颜色
        /// </summary>
        public static Color GridColor { get; set; } = Color.DarkCyan;
        /// <summary>
        /// 刻度颜色
        /// </summary>
        public static Brush ScaleColor { get; set; } = Brushes.Red;
        /// <summary>
        /// 标题颜色
        /// </summary>
        public static Brush TitleColor { get; set; } = Brushes.Black;
        /// <summary>
        /// 点-颜色
        /// </summary>
        public static Brush PointColor { get; set; } = Brushes.Black;
        /// <summary>
        /// 线颜色
        /// </summary>
        public static Color LineColor { get; set; } = Color.Red;
        /// <summary>
        /// 线条类型
        /// </summary>
        public static ELineType LineType { get; set; } = ELineType.Curve;
        /// <summary>
        /// 线条类型
        /// </summary>
        public enum ELineType
        {
            /// <summary>
            /// 折线
            /// </summary>
            Line = 1,
            /// <summary>
            /// 曲线
            /// </summary>
            Curve = 2
        }
        #endregion

        #region 数据配置
        /// <summary>
        /// X点集合
        /// </summary>
        public static List<float> Point_X { get; set; }
        /// <summary>
        /// Y点集合
        /// </summary>
        public static List<float> Point_Y { get; set; }
        #endregion
        /// <summary>
        /// 画图
        /// </summary>
        /// <returns></returns>
        public static Image Draw()
        {
            //根据数据最大值 自行调整X轴、Y轴最大值
            float maxX = Point_X.Max();
            float maxY = Point_Y.Max();
            //在刚好画出x轴、y轴的基础上再多预留1个刻度的边距
            if (maxX % Scale != 0)
            {
                maxX = maxX + maxX % Scale + Scale;
            }
            else
            {
                maxX = maxX + Scale;
            }
            if (maxY % Scale != 0)
            {
                maxY = maxY + maxY % Scale + Scale;
            }
            else
            {
                maxY = maxY + Scale;
            }
            Max_X = (int)maxX;
            Max_Y = (int)maxY;
            //图像整体比X轴宽 刻度*2、比Y轴高 刻度*2
            int imageWidth = Max_X + Margin;
            int imageHeigth = Max_Y + Margin;
            //均分边距
            int avgMargin = (int)Math.Floor(Convert.ToDecimal(Margin) / 2);
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeigth);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);
            Font font = new System.Drawing.Font("微软雅黑", 9, FontStyle.Regular);
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
               new Rectangle(0, 0, image.Width, image.Height),
               GridColor,
               GridColor,
               0F, true
               );
            //画背景
            g.FillRectangle(BackColor, 0, 0, imageWidth, imageHeigth);
            //X轴刻度总数
            int scaleXTotal = (int)Math.Floor(Convert.ToDecimal(Max_X) / Scale);
            //y轴刻度总数
            int scaleYTotal = (int)Math.Floor(Convert.ToDecimal(Max_Y) / Scale);
            if (IsShowGrid)
            {
                //画X轴网格
                System.Drawing.Pen scalePen = new Pen(brush, 1);
                for (int i = 0; i < scaleXTotal; i++)
                {
                    if (i % 2 == 0)
                    {
                        //偶数刻度线出头5px
                        g.DrawLine(scalePen, avgMargin + Scale * i, avgMargin, avgMargin + Scale * i, Max_Y + avgMargin + 5);
                    }
                    else
                    {
                        //奇数刻度线不出头
                        g.DrawLine(scalePen, avgMargin + Scale * i, avgMargin, avgMargin + Scale * i, Max_Y + avgMargin);
                    }
                }
                //画Y轴网格
                for (int i = 0; i < scaleYTotal; i++)
                {
                    if (i % 2 == 0)
                    {
                        //偶数刻度线出头5px
                        g.DrawLine(scalePen, avgMargin - 5, avgMargin + Scale * i, Max_X + avgMargin, avgMargin + Scale * i);
                    }
                    else
                    {
                        //奇数刻度线不出头
                        g.DrawLine(scalePen, avgMargin, avgMargin + Scale * i, Max_X + avgMargin, avgMargin + Scale * i);
                    }
                }
            }
            if (IsShowScale)
            {
                //画X轴刻度
                List<int> scaleXs = new List<int>();
                for (int i = 0; i < scaleXTotal + 1; i++)
                {
                    scaleXs.Add(Scale * i);
                }
                int tagx = 0;
                foreach (var item in scaleXs)
                {
                    if (tagx % 2 == 0)
                    {
                        //偶数刻度显示刻度值
                        g.DrawString(item.ToString(), font, ScaleColor, item + avgMargin, Max_Y + avgMargin + 5);
                    }
                    tagx++;
                }
                //画Y轴刻度
                List<int> scaleYs = new List<int>();
                for (int i = 0; i < scaleYTotal + 1; i++)
                {
                    scaleYs.Add(Scale * i);
                }
                int tagy = 0;
                foreach (var item in scaleYs)
                {
                    if (tagy % 2 == 0)
                    {
                        //偶数刻度显示刻度值
                        g.DrawString(item.ToString(), font, ScaleColor, 0, Max_Y - item + avgMargin);
                    }
                    tagy++;
                }
            }
            //画边框
            g.DrawRectangle(new Pen(BorderColor), avgMargin, avgMargin, Max_X, Max_Y);
            //绘制点
            List<PointF> points = new List<PointF>();
            if (Point_X.Count == Point_Y.Count && Point_X.Count != 0)
            {
                for (int i = 0; i < Point_X.Count; i++)
                {
                    PointF point = new PointF();
                    point.X = Point_X[i] + avgMargin;
                    point.Y = Max_Y - Point_Y[i] + avgMargin;
                    //绘制点的时候起始点需要减除 点大小的1/2 保证点在坐标中间
                    g.FillEllipse(PointColor, point.X - PointSize / 2, point.Y - PointSize / 2, PointSize, PointSize);
                    points.Add(point);
                }
            }
            //画线
            if (LineType == ELineType.Line)
            {
                g.DrawLines(new Pen(LineColor, LineSize), points.ToArray());//画折线
            }
            if (LineType == ELineType.Curve)
            {
                g.DrawCurve(new Pen(LineColor, LineSize), points.ToArray());//画曲线
            }
            //绘制标题
            if (Point_X.Count == Point_Y.Count && Point_X.Count != 0 && IsShowTitle)
            {
                for (int i = 0; i < Point_X.Count; i++)
                {
                    Font fontTitle = new System.Drawing.Font("微软雅黑", 7, FontStyle.Bold);
                    g.DrawString("(" + Point_X[i] + "," + Point_Y[i] + ")".ToString(), fontTitle, TitleColor, Point_X[i], Max_Y - Point_Y[i] + avgMargin);
                }
            }
            g.Dispose();
            return image;
        }
    }

}
